<template>
  <div class="ranking-management">
    <div class="page-header">
      <h1>排行榜管理</h1>
      <button @click="showAddRankingModal = true" class="btn-primary">
        <i class="icon-add"></i>
        添加排行榜
      </button>
    </div>

    <div class="content-layout">
      <!-- 左侧排行榜类型列表 -->
      <div class="ranking-sidebar">
        <div class="sidebar-header">
          <h2>排行榜类型</h2>
          <button @click="showAddRankingTypeModal = true" class="btn-add-ranking-type">
            <i class="icon-add"></i>
            添加类型
          </button>
        </div>
        <div class="ranking-type-list">
          <div 
            class="ranking-type-item"
            :class="{ 'active': selectedRankingTypeId === null }"
            @click="selectRankingType(null)"
          >
            <span class="ranking-type-name">全部排行榜</span>
            <span class="ranking-type-count">{{ totalRankings }}</span>
          </div>
          <div 
            v-for="type in rankingTypes" 
            :key="type.id"
            class="ranking-type-item"
            :class="{ 'active': selectedRankingTypeId === type.id }"
            @click="selectRankingType(type.id)"
          >
            <span class="ranking-type-name">{{ type.categoryName }}</span>
            <span class="ranking-type-count">{{ type.rankingCount || 0 }}</span>
            <div class="ranking-type-actions">
              <button @click.stop="editRankingType(type)" class="btn-icon" title="编辑">
                <i class="icon-edit"></i>
                编辑
              </button>
              <button @click.stop="deleteRankingType(type)" class="btn-icon" title="删除">
                <i class="icon-delete"></i>
                删除
              </button>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧排行榜列表 -->
      <div class="ranking-content">
        <div class="content-header">
          <h2>排行榜内容列表</h2>
          <div class="content-info">
            <span v-if="selectedRankingTypeId">
              当前显示: {{ getRankingTypeName(selectedRankingTypeId) }}
            </span>
            <span v-else>
              请选择排行榜类型
            </span>
          </div>
        </div>


        <!-- 排行榜列表表格 -->
        <div class="table-container">
          <table>
            <thead>
              <tr>
                <th>排名</th>
                <th>头像</th>
                <th>名称</th>
                <th>介绍</th>
                <th>状态</th>
                <th>操作</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(item, index) in rankingContentList" :key="item.id">
                <td>{{ item.rank || index + 1 }}</td>
                <td>
                  <img v-if="item.avatar" :src="item.avatar" :alt="item.rankName" class="avatar-img" />
                  <div v-else class="avatar-placeholder">无头像</div>
                </td>
                <td>{{ item.rankName }}</td>
                <td>{{ item.intro || '暂无介绍' }}</td>
                <td>
                  <span :class="['status-badge', getStatusBadgeClass(item.status)]">
                    {{ getStatusText(item.status) }}
                  </span>
                </td>
                <td>
                  <div v-if="item.status === 1" class="action-buttons">
                    <button @click="approveItem(item)" class="btn-approve" title="审核通过">
                      <i class="icon-check"></i>
                      通过
                    </button>
                    <button @click="rejectItem(item)" class="btn-reject" title="审核拒绝">
                      <i class="icon-close"></i>
                      拒绝
                    </button>
                  </div>
                </td>
              </tr>
            </tbody>
          </table>
          <div v-if="rankingContentList.length === 0" class="no-data">
            请选择排行榜类型查看内容
          </div>
        </div>
      </div>
    </div>

    <!-- 添加/编辑排行榜类型模态框 -->
    <div v-if="showAddRankingTypeModal || showEditRankingTypeModal" class="modal-overlay">
      <div class="modal modal-small">
        <div class="modal-header">
          <h2>{{ showEditRankingTypeModal ? '编辑排行榜类型' : '添加排行榜类型' }}</h2>
          <button @click="closeRankingTypeModal" class="btn-close">&times;</button>
        </div>
        <div class="modal-body">
          <form @submit.prevent="saveRankingType">
            <div class="form-group">
              <label for="rankingTypeName">类型名称</label>
              <input 
                type="text" 
                id="rankingTypeName" 
                v-model="rankingTypeForm.name" 
                required
              />
            </div>
            <div class="form-group">
              <label for="rankingTypeDescription">类型描述</label>
              <textarea 
                id="rankingTypeDescription" 
                v-model="rankingTypeForm.description" 
                rows="3"
              ></textarea>
            </div>
            <div class="form-actions">
              <button type="button" @click="closeRankingTypeModal" class="btn-cancel">
                取消
              </button>
              <button type="submit" class="btn-primary">
                {{ showEditRankingTypeModal ? '保存' : '添加' }}
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>

    <!-- 添加/编辑排行榜模态框 -->
    <div v-if="showAddRankingModal || showEditRankingModal" class="modal-overlay">
      <div class="modal">
        <div class="modal-header">
          <h2>{{ showEditRankingModal ? '编辑排行榜' : '添加排行榜' }}</h2>
          <button @click="closeRankingModal" class="btn-close">&times;</button>
        </div>
        <div class="modal-body">
          <form @submit.prevent="saveRanking">
            <div class="form-group">
              <label for="rankingName">名称</label>
              <input 
                type="text" 
                id="rankingName" 
                v-model="rankingForm.name" 
                required
              />
            </div>
            <div class="form-group">
              <label for="rankingType">类型</label>
              <select id="rankingType" v-model="rankingForm.typeId" required>
                <option value="">请选择类型</option>
                <option v-for="type in rankingTypes" :key="type.id" :value="type.id">
                  {{ type.categoryName }}
                </option>
              </select>
            </div>
            <div class="form-group">
              <label for="rankingDescription">描述</label>
              <textarea 
                id="rankingDescription" 
                v-model="rankingForm.description" 
                rows="3"
              ></textarea>
            </div>
            <div class="form-group">
              <label for="rankingUpdateFrequency">更新频率</label>
              <select id="rankingUpdateFrequency" v-model="rankingForm.updateFrequency" required>
                <option value="realtime">实时更新</option>
                <option value="hourly">每小时更新</option>
                <option value="daily">每日更新</option>
                <option value="weekly">每周更新</option>
                <option value="monthly">每月更新</option>
              </select>
            </div>
            <div class="form-group">
              <label for="rankingStatus">状态</label>
              <select id="rankingStatus" v-model="rankingForm.status" required>
                <option value="active">已激活</option>
                <option value="inactive">未激活</option>
              </select>
            </div>
            <div class="form-actions">
              <button type="button" @click="closeRankingModal" class="btn-cancel">
                取消
              </button>
              <button type="submit" class="btn-primary">
                {{ showEditRankingModal ? '保存' : '添加' }}
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>

    <!-- 排行榜详情模态框 -->
    <div v-if="showRankingDetailModal" class="modal-overlay">
      <div class="modal modal-large">
        <div class="modal-header">
          <h2>排行榜详情</h2>
          <button @click="showRankingDetailModal = false" class="btn-close">&times;</button>
        </div>
        <div class="modal-body">
          <div class="ranking-detail">
            <div class="detail-item">
              <span class="label">ID:</span>
              <span class="value">{{ selectedRanking.id }}</span>
            </div>
            <div class="detail-item">
              <span class="label">名称:</span>
              <span class="value">{{ selectedRanking.name }}</span>
            </div>
            <div class="detail-item">
              <span class="label">类型:</span>
              <span class="ranking-type-badge">
                {{ getRankingTypeName(selectedRanking.typeId) }}
              </span>
            </div>
            <div class="detail-item">
              <span class="label">描述:</span>
              <div class="value ranking-description">{{ selectedRanking.description }}</div>
            </div>
            <div class="detail-item">
              <span class="label">项目数:</span>
              <span class="value">{{ selectedRanking.itemCount || 0 }}</span>
            </div>
            <div class="detail-item">
              <span class="label">更新频率:</span>
              <span class="value">{{ getUpdateFrequencyText(selectedRanking.updateFrequency) }}</span>
            </div>
            <div class="detail-item">
              <span class="label">状态:</span>
              <span :class="['status-badge', selectedRanking.status]">
                {{ getStatusText(selectedRanking.status) }}
              </span>
            </div>
            <div class="detail-item">
              <span class="label">创建时间:</span>
              <span class="value">{{ formatDate(selectedRanking.createdAt) }}</span>
            </div>
            <div class="detail-item">
              <span class="label">最后更新:</span>
              <span class="value">{{ formatDate(selectedRanking.updatedAt) }}</span>
            </div>
          </div>

          <!-- 排行榜内容列表 -->
          <div class="ranking-items">
            <h3>排行榜内容列表</h3>
            <div class="table-container">
              <table>
                <thead>
                  <tr>
                    <th>排名</th>
                    <th>头像</th>
                    <th>名称</th>
                    <th>介绍</th>
                    <th>状态</th>
                    <th>操作</th>
                  </tr>
                </thead>
                <tbody>
                  <tr v-for="(item, index) in rankingContentList" :key="item.id">
                    <td>{{ item.rank || index + 1 }}</td>
                    <td>
                      <img v-if="item.avatar" :src="item.avatar" :alt="item.rankName" class="avatar-img" />
                      <div v-else class="avatar-placeholder">无头像</div>
                    </td>
                    <td>{{ item.rankName }}</td>
                    <td>{{ item.intro || '暂无介绍' }}</td>
                    <td>
                      <span :class="['status-badge', getStatusBadgeClass(item.status)]">
                        {{ getStatusText(item.status) }}
                      </span>
                    </td>
                    <td>
                      <div v-if="item.status === 1" class="action-buttons">
                        <button @click="approveItem(item)" class="btn-approve" title="审核通过">
                          <i class="icon-check"></i>
                          通过
                        </button>
                        <button @click="rejectItem(item)" class="btn-reject" title="审核拒绝">
                          <i class="icon-close"></i>
                          拒绝
                        </button>
                      </div>
                    </td>
                  </tr>
                </tbody>
              </table>
              <div v-if="rankingContentList.length === 0" class="no-data">
                暂无数据
              </div>
            </div>
          </div>
          
          <div class="modal-actions">
            <button @click="showRankingDetailModal = false" class="btn-primary">
              关闭
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { rankingApi } from '../../api/modules'

// 响应式数据
const rankings = ref([])
const rankingTypes = ref([])
const rankingItems = ref([])
const rankingContentList = ref([])
const selectedRankingTypeId = ref(null)
const searchQuery = ref('')
const statusFilter = ref('')
const sortFilter = ref('newest')
const currentPage = ref(1)
const pageSize = ref(20)
const totalRankings = ref(0)

// 模态框状态
const showAddRankingTypeModal = ref(false)
const showEditRankingTypeModal = ref(false)
const showAddRankingModal = ref(false)
const showEditRankingModal = ref(false)
const showRankingDetailModal = ref(false)
const selectedRanking = ref({})

// 排行榜类型表单
const rankingTypeForm = ref({
  id: null,
  name: '',
  description: ''
})

// 排行榜表单
const rankingForm = ref({
  id: null,
  name: '',
  description: '',
  typeId: '',
  updateFrequency: 'daily',
  status: 'active'
})

// 计算属性
const filteredRankings = computed(() => {
  let result = rankings.value

  // 类型过滤
  if (selectedRankingTypeId.value !== null) {
    result = result.filter(ranking => ranking.typeId === selectedRankingTypeId.value)
  }

  // 搜索过滤
  if (searchQuery.value) {
    const query = searchQuery.value.toLowerCase()
    result = result.filter(ranking => 
      ranking.name.toLowerCase().includes(query)
    )
  }

  // 状态过滤
  if (statusFilter.value) {
    result = result.filter(ranking => ranking.status === statusFilter.value)
  }

  // 排序
  if (sortFilter.value === 'newest') {
    result.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
  } else if (sortFilter.value === 'oldest') {
    result.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt))
  } else if (sortFilter.value === 'name') {
    result.sort((a, b) => a.name.localeCompare(b.name))
  }

  return result
})

const totalPages = computed(() => {
  return Math.ceil(totalRankings.value / pageSize.value)
})

// 生命周期钩子
onMounted(() => {
  fetchRankingTypes()
  fetchRankings()
})

// 方法
const fetchRankingTypes = async () => {
  try {
    // 添加分页参数，获取所有排行榜分类
    const params = {
      page: 1,
      pageSize: 100 // 设置较大的页面大小以获取所有数据
    }
    const response = await rankingApi.getRankingTypes(params)
    // 处理分页接口返回的数据格式
    rankingTypes.value = response.data?.list || []
  } catch (error) {
    console.error('获取排行榜类型失败:', error)
    // 如果API调用失败，使用模拟数据作为后备
    rankingTypes.value = [
      { id: 1, name: '用户活跃度', description: '基于用户活跃度的排行榜', rankingCount: 5 },
      { id: 2, name: '帖子热度', description: '基于帖子热度的排行榜', rankingCount: 3 },
      { id: 3, name: '积分排行', description: '基于用户积分的排行榜', rankingCount: 2 },
      { id: 4, name: '贡献值排行', description: '基于用户贡献值的排行榜', rankingCount: 1 }
    ]
  }
}

const fetchRankings = async () => {
  try {
    const params = {
      page: currentPage.value,
      pageSize: pageSize.value,
      typeId: selectedRankingTypeId.value,
      keyword: searchQuery.value,
      status: statusFilter.value,
      sort: sortFilter.value
    }
    
    const response = await rankingApi.getRankingsPage(params)
    rankings.value = response.data?.list || []
    totalRankings.value = response.data?.total || 0
  } catch (error) {
    console.error('获取排行榜列表失败:', error)
    // 如果API调用失败，使用模拟数据作为后备
    rankings.value = [
      {
        id: 1,
        name: '本周活跃用户榜',
        description: '统计本周最活跃的用户',
        typeId: 1,
        itemCount: 10,
        updateFrequency: 'weekly',
        status: 'active',
        createdAt: '2023-11-20T10:30:00Z',
        updatedAt: '2023-11-20T10:30:00Z'
      },
      {
        id: 2,
        name: '本月热门帖子榜',
        description: '统计本月最热门的帖子',
        typeId: 2,
        itemCount: 20,
        updateFrequency: 'monthly',
        status: 'active',
        createdAt: '2023-11-15T14:20:00Z',
        updatedAt: '2023-11-20T09:15:00Z'
      },
      {
        id: 3,
        name: '用户积分总榜',
        description: '统计用户积分排名',
        typeId: 3,
        itemCount: 50,
        updateFrequency: 'daily',
        status: 'active',
        createdAt: '2023-11-10T08:45:00Z',
        updatedAt: '2023-11-20T08:00:00Z'
      },
      {
        id: 4,
        name: '社区贡献榜',
        description: '统计用户对社区的贡献',
        typeId: 4,
        itemCount: 30,
        updateFrequency: 'weekly',
        status: 'inactive',
        createdAt: '2023-11-05T16:30:00Z',
        updatedAt: '2023-11-18T16:30:00Z'
      }
    ]
    totalRankings.value = rankings.value.length
  }
}

const fetchRankingItems = async (rankingId) => {
  try {
    const response = await rankingApi.getRankingItems(rankingId)
    rankingItems.value = response.data || []
  } catch (error) {
    console.error('获取排行榜项目失败:', error)
    // 如果API调用失败，使用模拟数据作为后备
    rankingItems.value = [
      { id: 1, name: '张三', score: 9850, updatedAt: '2023-11-20T10:30:00Z' },
      { id: 2, name: '李四', score: 9720, updatedAt: '2023-11-20T10:30:00Z' },
      { id: 3, name: '王五', score: 9680, updatedAt: '2023-11-20T10:30:00Z' },
      { id: 4, name: '赵六', score: 9520, updatedAt: '2023-11-20T10:30:00Z' },
      { id: 5, name: '钱七', score: 9450, updatedAt: '2023-11-20T10:30:00Z' }
    ]
  }
}

const fetchRankingContentList = async (categoryId) => {
  try {
    // 如果没有选择分类ID，则不获取内容列表
    if (!categoryId) {
      rankingContentList.value = []
      return
    }
    
    const params = {
      categoryId: categoryId,
      page: 1,
      pageSize: 50 // 设置较大的页面大小以获取更多数据
    }
    
    const response = await rankingApi.getRankingPersonPage(params)
    rankingContentList.value = response.data?.list || []
  } catch (error) {
    console.error('获取排行榜内容列表失败:', error)
    // 如果API调用失败，使用模拟数据作为后备
    rankingContentList.value = [
      { 
        id: 1, 
        rankName: '张三', 
        intro: '这是张三的介绍', 
        avatar: 'https://example.com/avatar1.jpg',
        rank: 1,
        status: 0
      },
      { 
        id: 2, 
        rankName: '李四', 
        intro: '这是李四的介绍', 
        avatar: 'https://example.com/avatar2.jpg',
        rank: 2,
        status: 1
      },
      { 
        id: 3, 
        rankName: '王五', 
        intro: '这是王五的介绍', 
        avatar: 'https://example.com/avatar3.jpg',
        rank: 3,
        status: 0
      },
      { 
        id: 4, 
        rankName: '赵六', 
        intro: '这是赵六的介绍', 
        avatar: 'https://example.com/avatar4.jpg',
        rank: 4,
        status: 1
      }
    ]
  }
}

const selectRankingType = (typeId) => {
  selectedRankingTypeId.value = typeId
  currentPage.value = 1
  fetchRankings()
  fetchRankingContentList(typeId)
}

const handleSearch = () => {
  currentPage.value = 1
}

const filterRankings = () => {
  currentPage.value = 1
  fetchRankings()
}

const prevPage = () => {
  if (currentPage.value > 1) {
    currentPage.value--
    fetchRankings()
  }
}

const nextPage = () => {
  if (currentPage.value < totalPages.value) {
    currentPage.value++
    fetchRankings()
  }
}

// 排行榜类型相关方法
const editRankingType = (type) => {
  rankingTypeForm.value = {
    id: type.id,
    name: type.categoryName,
    description: type.description
  }
  showEditRankingTypeModal.value = true
}

const saveRankingType = async () => {
  try {
    if (showEditRankingTypeModal.value) {
      // 编辑排行榜类型
      const requestData = {
        categoryName: rankingTypeForm.value.name,
        description: rankingTypeForm.value.description
      }
      await rankingApi.updateRankingType(rankingTypeForm.value.id, requestData)
    } else {
      // 创建排行榜分类
      const requestData = {
        categoryName: rankingTypeForm.value.name,
        description: rankingTypeForm.value.description
      }
      await rankingApi.createRankingType(requestData)
    }
    closeRankingTypeModal()
    fetchRankingTypes()
  } catch (error) {
    console.error('保存排行榜类型失败:', error)
    alert('保存排行榜类型失败，请稍后再试')
  }
}

const deleteRankingType = async (type) => {
  if (confirm(`确定要删除排行榜类型 ${type.categoryName} 吗？`)) {
    try {
      await rankingApi.deleteRankingType(type.id)
      fetchRankingTypes()
      if (selectedRankingTypeId.value === type.id) {
        selectedRankingTypeId.value = null
        fetchRankings()
      }
    } catch (error) {
      console.error('删除排行榜类型失败:', error)
      alert('删除排行榜类型失败，请稍后再试')
    }
  }
}

const closeRankingTypeModal = () => {
  showAddRankingTypeModal.value = false
  showEditRankingTypeModal.value = false
  rankingTypeForm.value = {
    id: null,
    name: '',
    description: ''
  }
}

// 排行榜相关方法
const viewRanking = (ranking) => {
  selectedRanking.value = { ...ranking }
  fetchRankingItems(ranking.id)
  // 获取该排行榜类型的内容列表
  fetchRankingContentList(ranking.typeId)
  showRankingDetailModal.value = true
}

const editRanking = (ranking) => {
  rankingForm.value = { ...ranking }
  showEditRankingModal.value = true
}

const saveRanking = async () => {
  try {
    if (showEditRankingModal.value) {
      // 编辑排行榜
      await rankingApi.updateRanking(rankingForm.value.id, rankingForm.value)
    } else {
      // 创建排行榜
      await rankingApi.createRanking(rankingForm.value)
    }
    closeRankingModal()
    fetchRankings()
  } catch (error) {
    console.error('保存排行榜失败:', error)
    alert('保存排行榜失败，请稍后再试')
  }
}

const deleteRanking = async (ranking) => {
  if (confirm(`确定要删除排行榜 ${ranking.name} 吗？此操作不可恢复！`)) {
    try {
      await rankingApi.deleteRanking(ranking.id)
      fetchRankings()
    } catch (error) {
      console.error('删除排行榜失败:', error)
      alert('删除排行榜失败，请稍后再试')
    }
  }
}

const activateRanking = async (ranking) => {
  try {
    await rankingApi.activateRanking(ranking.id)
    fetchRankings()
  } catch (error) {
    console.error('激活排行榜失败:', error)
    alert('激活排行榜失败，请稍后再试')
  }
}

const deactivateRanking = async (ranking) => {
  try {
    await rankingApi.deactivateRanking(ranking.id)
    fetchRankings()
  } catch (error) {
    console.error('停用排行榜失败:', error)
    alert('停用排行榜失败，请稍后再试')
  }
}

const refreshRanking = async (ranking) => {
  try {
    await rankingApi.refreshRanking(ranking.id)
    alert('排行榜数据已刷新')
    fetchRankings()
  } catch (error) {
    console.error('刷新排行榜失败:', error)
    alert('刷新排行榜失败，请稍后再试')
  }
}

const closeRankingModal = () => {
  showAddRankingModal.value = false
  showEditRankingModal.value = false
  rankingForm.value = {
    id: null,
    name: '',
    description: '',
    typeId: '',
    updateFrequency: 'daily',
    status: 'active'
  }
}

// 辅助方法
const getRankingTypeName = (typeId) => {
  const type = rankingTypes.value.find(t => t.id === typeId)
  return type ? type.categoryName : '未分类'
}

const getStatusText = (status) => {
  const statusMap = {
    0: '正常',
    1: '待审核',
    2: '已拒绝'
  }
  return statusMap[status] || '未知'
}

const getStatusBadgeClass = (status) => {
  const classMap = {
    0: 'active',
    1: 'pending',
    2: 'rejected'
  }
  return classMap[status] || 'unknown'
}

const getUpdateFrequencyText = (frequency) => {
  const frequencyMap = {
    realtime: '实时更新',
    hourly: '每小时更新',
    daily: '每日更新',
    weekly: '每周更新',
    monthly: '每月更新'
  }
  return frequencyMap[frequency] || frequency
}

const formatDate = (dateString) => {
  if (!dateString) return '从未'
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN')
}

// 审核功能
const approveItem = async (item) => {
  try {
    await rankingApi.reviewRankingItem(item.id, 0)
    // 更新本地数据状态
    item.status = 0
    // 刷新列表
    fetchRankingContentList(selectedRankingTypeId.value)
    alert('审核通过成功')
  } catch (error) {
    console.error('审核通过失败:', error)
    alert('审核通过失败，请稍后再试')
  }
}

const rejectItem = async (item) => {
  try {
    await rankingApi.reviewRankingItem(item.id, 2)
    // 更新本地数据状态
    item.status = 2
    // 刷新列表
    fetchRankingContentList(selectedRankingTypeId.value)
    alert('审核拒绝成功')
  } catch (error) {
    console.error('审核拒绝失败:', error)
    alert('审核拒绝失败，请稍后再试')
  }
}
</script>

<style scoped>
.ranking-management {
  padding: 24px;
  background-color: #f8fafc;
  min-height: 100vh;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.page-header h1 {
  color: #1e293b;
  font-size: 24px;
  font-weight: 600;
  margin: 0;
}

.btn-primary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background-color: #3b82f6;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s;
}

.btn-primary:hover {
  background-color: #2563eb;
}

/* 内容布局 */
.content-layout {
  display: flex;
  gap: 24px;
}

/* 左侧排行榜类型栏 */
.ranking-sidebar {
  width: 280px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  height: fit-content;
  position: sticky;
  top: 24px;
}

.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #e5e7eb;
}

.sidebar-header h2 {
  margin: 0;
  color: #1f2937;
  font-size: 16px;
  font-weight: 600;
}

.btn-add-ranking-type {
  min-width: 32px;
  height: 32px;
  padding: 0 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  background-color: #eff6ff;
  color: #3b82f6;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 0.2s;
  font-size: 12px;
  white-space: nowrap;
}

.btn-add-ranking-type:hover {
  background-color: #dbeafe;
}

.ranking-type-list {
  padding: 8px;
}

.ranking-type-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
  margin-bottom: 4px;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.ranking-type-item:hover {
  background-color: #f9fafb;
}

.ranking-type-item.active {
  background-color: #eff6ff;
  color: #3b82f6;
}

.ranking-type-name {
  flex: 1;
  font-size: 14px;
  font-weight: 500;
}

.ranking-type-count {
  padding: 2px 8px;
  background-color: #f3f4f6;
  border-radius: 12px;
  font-size: 12px;
  color: #6b7280;
  margin-right: 8px;
}

.ranking-type-item.active .ranking-type-count {
  background-color: #dbeafe;
  color: #3b82f6;
}

.ranking-type-actions {
  display: none;
  gap: 4px;
}

.ranking-type-item:hover .ranking-type-actions {
  display: flex;
}

.btn-icon {
  min-width: 24px;
  height: 24px;
  padding: 0 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: none;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s;
  font-size: 10px;
  white-space: nowrap;
}

.btn-icon:hover {
  background-color: #e5e7eb;
}

/* 右侧排行榜内容 */
.ranking-content {
  flex: 1;
}

/* 搜索和筛选区域 */
.filter-section {
  display: flex;
  justify-content: space-between;
  margin-bottom: 24px;
  gap: 16px;
}

.search-box {
  position: relative;
  flex: 1;
  max-width: 400px;
}

.search-box input {
  width: 100%;
  padding: 10px 16px 10px 40px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 14px;
  background-color: white;
}

.search-box .icon-search {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: #6b7280;
}

.filter-options {
  display: flex;
  gap: 12px;
}

.filter-options select {
  padding: 10px 16px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 14px;
  background-color: white;
  min-width: 120px;
}

/* 表格样式 */
.table-container {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  margin-bottom: 24px;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  padding: 12px 16px;
  text-align: left;
  border-bottom: 1px solid #e5e7eb;
}

th {
  background-color: #f9fafb;
  color: #374151;
  font-weight: 600;
  font-size: 14px;
}

td {
  color: #1f2937;
  font-size: 14px;
}

tr:hover {
  background-color: #f9fafb;
}

/* 排行榜标题 */
.ranking-title {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* 排行榜类型徽章 */
.ranking-type-badge {
  display: inline-block;
  padding: 4px 8px;
  background-color: #eff6ff;
  color: #1e40af;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

/* 状态徽章 */
.status-badge {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.status-badge.active {
  background-color: #dcfce7;
  color: #166534;
}

.status-badge.pending {
  background-color: #fef3c7;
  color: #92400e;
}

.status-badge.rejected {
  background-color: #fee2e2;
  color: #991b1b;
}

.status-badge.unknown {
  background-color: #f3f4f6;
  color: #6b7280;
}

/* 操作按钮 */
.action-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.action-buttons button {
  min-width: 32px;
  height: 32px;
  padding: 0 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s;
  font-size: 12px;
  white-space: nowrap;
}

.btn-view {
  background-color: #eff6ff;
  color: #1e40af;
}

.btn-view:hover {
  background-color: #dbeafe;
}

.btn-edit {
  background-color: #f0fdf4;
  color: #166534;
}

.btn-edit:hover {
  background-color: #dcfce7;
}

.btn-activate {
  background-color: #dcfce7;
  color: #166534;
}

.btn-activate:hover {
  background-color: #bbf7d0;
}

.btn-deactivate {
  background-color: #f3f4f6;
  color: #6b7280;
}

.btn-deactivate:hover {
  background-color: #e5e7eb;
}

.btn-refresh {
  background-color: #fef3c7;
  color: #92400e;
}

.btn-refresh:hover {
  background-color: #fde68a;
}

.btn-delete {
  background-color: #fef2f2;
  color: #991b1b;
}

.btn-delete:hover {
  background-color: #fee2e2;
}

.btn-approve {
  background-color: #dcfce7;
  color: #166534;
}

.btn-approve:hover {
  background-color: #bbf7d0;
}

.btn-reject {
  background-color: #fee2e2;
  color: #991b1b;
}

.btn-reject:hover {
  background-color: #fecaca;
}

/* 分页控件 */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
}

.btn-page {
  padding: 8px 16px;
  background-color: white;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  color: #374151;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-page:hover:not(:disabled) {
  background-color: #f9fafb;
}

.btn-page:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.page-info {
  color: #6b7280;
  font-size: 14px;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background-color: white;
  border-radius: 8px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
}

.modal-small {
  max-width: 400px;
}

.modal-large {
  max-width: 900px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  border-bottom: 1px solid #e5e7eb;
}

.modal-header h2 {
  margin: 0;
  color: #1f2937;
  font-size: 18px;
  font-weight: 600;
}

.btn-close {
  background: none;
  border: none;
  font-size: 24px;
  color: #6b7280;
  cursor: pointer;
  padding: 0;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-body {
  padding: 24px;
}

/* 表单样式 */
.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #374151;
  font-weight: 500;
  font-size: 14px;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
}

.form-group textarea {
  resize: vertical;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 24px;
}

.btn-cancel {
  padding: 8px 16px;
  background-color: white;
  color: #374151;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s;
}

.btn-cancel:hover {
  background-color: #f9fafb;
}

/* 排行榜详情样式 */
.ranking-detail {
  margin-bottom: 24px;
}

.detail-item {
  display: flex;
  padding: 12px 0;
  border-bottom: 1px solid #f3f4f6;
}

.detail-item:last-child {
  border-bottom: none;
}

.detail-item .label {
  width: 100px;
  color: #6b7280;
  font-weight: 500;
}

.detail-item .value {
  flex: 1;
  color: #1f2937;
}

.ranking-description {
  white-space: pre-wrap;
  word-break: break-word;
}

/* 排行榜内容列表 */
.ranking-items {
  margin-top: 24px;
}

.ranking-items h3 {
  margin-bottom: 16px;
  color: #1f2937;
  font-size: 16px;
  font-weight: 600;
}

.no-data {
  text-align: center;
  padding: 20px;
  color: #6b7280;
  font-size: 14px;
}

.avatar-img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
}

.avatar-placeholder {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #f3f4f6;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #6b7280;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 24px;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .content-layout {
    flex-direction: column;
  }
  
  .ranking-sidebar {
    width: 100%;
    position: static;
  }
}

@media (max-width: 768px) {
  .filter-section {
    flex-direction: column;
  }
  
  .search-box {
    max-width: none;
  }
  
  .filter-options {
    width: 100%;
  }
  
  .filter-options select {
    flex: 1;
  }
  
  .action-buttons {
    flex-wrap: wrap;
  }
  
  .table-container {
    overflow-x: auto;
  }
}
</style>